package com.onion.config;

import com.onion.MyJsonProperties;
import com.onion.MyJsonService;
import lombok.AllArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

@Configuration(proxyBeanMethods = false)
@AllArgsConstructor
@ConditionalOnClass(value = MyJsonService.class)
//EnableConfigurationProperties这个注解的意义大概相当于把value中的class实例注入到当前的实例中
@EnableConfigurationProperties(value = MyJsonProperties.class)
public class MyJsonConfig {

    private final MyJsonProperties myJsonProperties;

    @Bean
    @Lazy
    @ConditionalOnMissingBean(MyJsonService.class)
    public MyJsonService getMyJsonService() {
        MyJsonService myJsonService = new MyJsonService();
        myJsonService.setPrefixName(myJsonProperties.getPrefixName());
        myJsonService.setSuffixName(myJsonProperties.getSuffixName());
        return myJsonService;
    }
}
